package com.youu.youu.restapi.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.youu.youu.manager.bean.Message;
import com.youu.youu.manager.bean.MessageModel;
import com.youu.youu.manager.dao.MessageMapper;
import com.youu.youu.manager.dao.MessageModelMapper;
import com.youu.youu.restapi.service.MessageService;

@Service
public class MessageServiceImpl implements MessageService {

	@Autowired
	MessageMapper messageMapper;
	
	@Autowired
	MessageModelMapper messageModelMapper;
	
	@Override
	public List<Message> getMsgList(Integer uid) {
		// TODO Auto-generated method stub
		return messageMapper.getMsgBydate(uid);
	}

	@Override
	public boolean addMsg(Integer uid, Integer mid, String[] params) {
		// TODO Auto-generated method stub
		// 获取消息模版
		MessageModel messageModel = messageModelMapper.selectByPrimaryKey(mid);
		String modelConten = messageModel.getModelConten();
		StringBuffer messageSB = new StringBuffer(modelConten);
		
		int index = 0;
		// 给消息模版中传参数
		for (String param : params) {
			index = messageSB.indexOf("&", index);
			messageSB.replace(index, index+1, param);
			index += 1;
		}
		
		String msgContent = messageSB.toString();
		Message message = new Message(uid, mid, msgContent, new Date(), 0);
		int i = messageMapper.insertSelective(message);
		return i == 1;
	}

	@Override
	public boolean update(Integer msgid) {
		// TODO Auto-generated method stub
		Message record = new Message();
		record.setId(msgid);
		record.setStatus(1);
		int i = messageMapper.updateByPrimaryKeySelective(record);
		return i == 1;
	}

}
